package com.swordget.controller;


import com.alibaba.fastjson.JSONArray;
import com.swordget.entity.TableDesign;
import com.swordget.entity.po.DataQueryPo;
import com.swordget.entity.vo.*;
import com.swordget.service.DataHandlerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: David
 * @Date: Created in 14:10 2020/4/8
 * @Description: 数据查询服务
 * @Modified By:
 * @Version: 0.0.1
 */

@RestController
@RequestMapping("/query")
public class DataHandlerController {

    private DataHandlerService dataQueryService;

    @Autowired
    public DataHandlerController(DataHandlerService dataQueryService) {
        this.dataQueryService = dataQueryService;
    }

    /**
     * 查询指定表数据
     * @param pageVo
     * @return
     */
    @GetMapping
    public SimplePageResponseVo dataQuery(SimplePageRequestVo pageVo){
        //处理vo类
        DataQueryPo dataQueryPo = dataQueryService.dataQuery(pageVo);
        return SimplePageResponseVo.builder()
                .total(dataQueryPo.getTotal())
                .rows(dataQueryPo.getData())
                .build();
    }


    /**
     * 新增指定表数据
     * @param requestVo
     * @return
     */
    @PostMapping
    public SimpleResponseVo dataAdd(@RequestBody SimpleRequestVo<FromRequestVo<InputStructVo>> requestVo) {
        final FromRequestVo fromRequestVo = requestVo.getData();
        final TableDesign tableDesign = fromRequestVo.getTable();
        final List<InputStructVo> inputStructVos = fromRequestVo.getResult();
        final String loginId = requestVo.getLoginId();
        final String orgId = requestVo.getOrgId();
        boolean isAdd = dataQueryService.dataAdd(tableDesign, inputStructVos, loginId, orgId);
        if (isAdd) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 修改指定表数据
     * @param requestVo
     * @return
     */
    @PutMapping
    public SimpleResponseVo dataModify(@RequestBody SimpleRequestVo<FromRequestVo<InputStructVo>> requestVo) {
        final FromRequestVo fromRequestVo = requestVo.getData();
        final TableDesign tableDesign = fromRequestVo.getTable();
        final List<InputStructVo> inputStructVos = fromRequestVo.getResult();
        final String loginId = requestVo.getLoginId();
        final String orgId = requestVo.getOrgId();
        boolean isModify = dataQueryService.dataModify(tableDesign, inputStructVos, loginId, orgId);
        if (isModify) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 删除指定表数据
     * @param requestVo
     * @return
     */
    /*@DeleteMapping
    public SimpleResponseVo dataRemove(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        boolean isRemove = dataQueryService.dataRemove(tableDesign, list, loginId);
        if (isRemove) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }*/

    /**
     * 申请修改
     * @param requestVo
     * @return
     */
    @PutMapping("/applyModify")
    public SimpleResponseVo applyModify(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String dataStatus = requestVo.getData().getDataStatus();
        boolean f = dataQueryService.applyModify(tableDesign, searchParams, list, loginId, dataStatus);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }


    /**
     * 申请删除
     * @param requestVo
     * @return
     */
    @PutMapping("/applyRemove")
    public SimpleResponseVo dataRemove(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String dataStatus = requestVo.getData().getDataStatus();
        boolean f = dataQueryService.applyRemove(tableDesign, searchParams, list, loginId, dataStatus);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 提交
     * @param requestVo
     * @return
     */
    @PutMapping("/submit")
    public SimpleResponseVo submit(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String dataStatus = requestVo.getData().getDataStatus();
        boolean f = dataQueryService.dataSubmit(tableDesign, searchParams,list, loginId, dataStatus);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 审核不通过
     * @param requestVo
     * @return
     */
    @PutMapping("/notPass")
    public SimpleResponseVo notPass(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String dataStatus = requestVo.getData().getDataStatus();
        final String notPassMark = requestVo.getData().getNotPassMark();
        /*boolean f = dataQueryService.notPass(tableDesign, searchParams,list, dataStatus, loginId, notPassMark);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");*/
        Integer f = dataQueryService.notPass(tableDesign, searchParams,list, dataStatus, loginId, notPassMark);
        if (f == 3) {
            return SimpleResponseVo.success();
        } else if(f == 2){
            return SimpleResponseVo.fail("处理失败,请联系管理员");
        } else if(f == 1){
            return SimpleResponseVo.fail("处理失败,数据中包含有操作名的数据");
        }
        return SimpleResponseVo.fail("处理失败,没有选中的数据");

    }

    /**
     * 同意申请
     * @param requestVo
     * @return
     */
    @PutMapping("/agreeApply")
    public SimpleResponseVo agreeApply(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String orgId = requestVo.getOrgId();
        final String dataStatus = requestVo.getData().getDataStatus();
        boolean f = dataQueryService.agreeApply(tableDesign, searchParams,list, loginId, dataStatus, orgId);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 拒绝删除
     * @param requestVo
     * @return
     */
    @PutMapping("/rejectApply")
    public SimpleResponseVo rejectApply(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {
        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String dataStatus = requestVo.getData().getDataStatus();
        boolean f = dataQueryService.rejectApply(tableDesign, searchParams,list, loginId, dataStatus);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    @PutMapping("/pass")
    public SimpleResponseVo pass(@RequestBody SimpleRequestVo<FromRequestVo<String>> requestVo) {

        final TableDesign tableDesign = requestVo.getData().getTable();
        //搜索条件
        JSONArray searchParams = requestVo.getData().getQueryParams();
        //id集合
        final List<String> list = requestVo.getData().getResult();
        final String loginId = requestVo.getLoginId();
        final String orgId = requestVo.getOrgId();
        final String dataStatus = requestVo.getData().getDataStatus();
       /* boolean f = dataQueryService.pass(tableDesign, searchParams,list, loginId, dataStatus, orgId);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");*/
        Integer f = dataQueryService.pass(tableDesign, searchParams,list, loginId, dataStatus);
        if (f == 3) {
            return SimpleResponseVo.success();
        } else if(f == 2){
            return SimpleResponseVo.fail("处理失败,请联系管理员");
        } else if(f == 1){
            return SimpleResponseVo.fail("处理失败,数据中包含有操作名的数据");
        }
        return SimpleResponseVo.fail("处理失败,没有选中的数据");

    }


}
